<?php


namespace App\Http\Controllers\Order;


use App\Excel\Export\OrderExport;
use App\Excel\Export\OrderTrackingExport;
use App\Http\Controllers\Controller;
use App\Http\Search\Order\ListIndexSearch;
use App\Jobs\DeclareOrder;
use App\Libraries\Source\Import\Client;
use App\Repositories\Order;
use Illuminate\Http\Request;

class ListController extends Controller
{
    /**
     * @var Order
     * @author luffyzhao@vip.126.com
     */
    private $order;

    /**
     * ListController constructor.
     * @param Order $order
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * @param ListIndexSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(ListIndexSearch $search){
        return $this->response(
            $this->order->simplePaginate($search->toArray())
        );
    }

    /**
     * @param ListIndexSearch $search
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
     * @author luffyzhao@vip.126.com
     */
    public function export(ListIndexSearch $search){
        return (new OrderExport($search))->download('product.xlsx');
    }

    /**
     * @param ListIndexSearch $search
     * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\BinaryFileResponse
     * @author luffyzhao@vip.126.com
     */
    public function export_tracking(ListIndexSearch $search){
        return (new OrderTrackingExport($search))->download('product.xlsx');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function import(Request $request){
        $uploadedFile = $request->file('file');
        try{
            $client = new Client(
                $uploadedFile->getRealPath()
            );
            return $this->response([
                'error' => $client->check()
            ] );
        }catch (\Exception $exception){
            return $this->response([
                'error' => ['请检查文件是否正确']
            ]);
        }
    }

    /**
     * @param $id
     * @author luffyzhao@vip.126.com
     */
    public function redeclare($id){
        DeclareOrder::dispatch(
            $this->order->find($id)
        );
    }

}